class ProductController < ApplicationController
  before_filter :user_authorize
  before_filter :sys_before_log
  #before_filter :privilege_filter
  
  def editProduct
    
  end

  def newProduct
    
  end

  def productList
    
  end

  def productListForApplicant
    
  end

  def productListForAuditor
    
  end

  def productListForVendor

  end

  def productOnlyView
    
  end

  def productView
    
  end

  def productViewOfBack
    
  end

  def rejectReason
    
  end

  def rejectReasonView

  end


  #得到所有的通过审批的，且没有被锁定的产品
  def getAuditedProducts
    if session[:auditor_id] !=nil || session[:applicant_id]!=nil
      pageBegin =  params[:start]
      pageEnd =  params[:limit]
      if pageBegin == nil
        pageBegin = 0
        pageEnd = Constants::PAGE_SIZE
      end
      products = Product.findProductsOfPass(params[:product_name].to_s,params[:vendor_name].to_s,pageBegin,pageEnd)
      jsonstr="{totalProperty:"+products.length.to_s+",root:"+products.to_json(:include => {:vendor => {:only => :company_name}},:only => [:id,:name,:brand,:model,:specification,:manufacturer,:unit_price,:auditing_status])+"}"
      logger.info jsonstr
      render :text=>jsonstr, :layout=>false
    end
  end

  #得到所有未通过审批的产品
  def getProductsForAuditor
    pageBegin =  params[:start]
    pageEnd =  params[:limit]
    products = Product.findProductsForAuditor(params[:product_name].to_s,params[:vendor_name].to_s,pageBegin,pageEnd)
    jsonstr="{totalProperty:"+products.length.to_s+",root:"+products.to_json(:include => {:vendor => {:only => :company_name}},:only => [:id,:name,:brand,:model,:specification,:manufacturer,:unit_price,:auditing_status])+"}"
    #logger.info jsonstr
    render :text=>jsonstr, :layout=>false
  end

  #得到登录供应商自己的产品信息
  def getProductsOfVendor
    pageBegin =  params[:start]
    pageEnd =  params[:limit]
    products = Product.findProductsOfVendor(session[:vendor_id],params[:name].to_s,params[:state].to_s,pageBegin,pageEnd)
    jsonstr="{totalProperty:"+products.length.to_s+",root:"+products.to_json(:only => [:id,:name,:brand,:model,:specification,:manufacturer,:unit_price,:auditing_status,:reject_reason,:reject_reason])+"}"
    #jsonstr = Product.findProductsOfVendor(session[:vendor_id],params[:name].to_s,params[:state].to_s)
    logger.info jsonstr
    render :text=>jsonstr, :layout=>false
  end

  #供应商添加产品
  def create
    unless request.get?
      begin
        @fileName = params[:file].original_filename.last(3)
        if @fileName == 'jpg'||@fileName == 'gif'||@fileName == 'bmp'||@fileName == 'JPG'||@fileName == 'GIF'||@fileName == 'BMP'
          if params[:file].size<=500*1024
            product = Product.new
            product.vendor_id = session[:vendor_id]
            product.name = params[:name]
            product.model = params[:model]
            product.brand = params[:brand]
            product.manufacturer = params[:manufacturer]
            product.specification = params[:specification]
            product.unit_price = params[:unit_price]
            product.photo = params[:file].read
            product.technology_data = params[:technology_data]
            product.auditing_status = '通过'
            product.save!
            info = 'success'
          else
            info = '产品图片过大,请确保图片大小在500KB以下'
          end
        else
          info = '产品图片格式错误,只能上传jpg gif和bmp格式图片'
        end
      rescue Exception => e
        info = e.to_s
      end
      result = "{success:true,info:\"#{info}\"}"
      render :text=>result, :layout=>false
    end
  end

  #供应商删除自己的产品 id not in (ids)
  def delete
    if session[:vendor_id] !=nil
      begin
        ids= params[:id][1..params[:id].length-2].split(',');
        for id in ids
          product = Product.find(id)
          if product.vendor_id == session[:vendor_id]
            product.destroy
          end
        end
        info = 'success'
      rescue Exception=>e
        info = e.to_s
      end
    end
    
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #获取指定ID的产品信息
  def edit
    product = Product.find(params[:id])
    render :text=>product.to_json(:include => {:vendor => {:only => :company_name}},:only => [:id,:name,:brand,:model,:specification,:manufacturer,:unit_price,:technology_data]),:layout=>false
  end

  #获得产品图片
  def getPhoto
    product = Product.find(params[:id])
    render :text=>product.photo,:layout=>false
  end

  #供应商更新产品信息
  def update
    begin
      if params[:file]==nil
        product = Product.find(params[:id])
        product.name = params[:name]
        product.model = params[:model]
        product.brand = params[:brand]
        product.manufacturer = params[:manufacturer]
        product.specification = params[:specification]
        product.unit_price = params[:unit_price]
        product.technology_data = params[:technology_data]
        product.reject_reason = nil
        product.auditing_status = '通过'
        product.save!
        info = 'success'
      else
        @fileName = params[:file].original_filename.last(3)
        if @fileName == 'jpg'||@fileName == 'gif'||@fileName == 'bmp'||@fileName == 'JPG'||@fileName == 'GIF'||@fileName == 'BMP'
          if params[:file].length<=500*1024
            product = Product.find(params[:id])
            product.name = params[:name]
            product.model = params[:model]
            product.brand = params[:brand]
            product.manufacturer = params[:manufacturer]
            product.specification = params[:specification]
            product.unit_price = params[:unit_price]
            product.photo = params[:file].read
            product.technology_data = params[:technology_data]
            product.reject_reason = nil
            product.auditing_status = '通过'
            product.save!
            info = 'success'
          else
            info = '产品图片过大,请确保图片大小在500KB以下'
          end
        else
          info = '产品图片格式错误,只能上传jpg gif和bmp格式图片'
        end
      end

    rescue Exception=>e
      logger.info e.to_s
      info = '更新产品信息出错，请重新操作。'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #审批人通过审批产品
  def auditProduct
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        product = Product.find(id)
        product.auditing_status = '通过'
        product.auditor_id = session[:auditor_id]
        product.save
      end
      info = 'success'
    rescue Exception=>e
      info = e.to_s
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #审批人驳回产品
  def rejectProduct
    begin
      ids= params[:id][1..params[:id].length-2].split(',');
      for id in ids
        product = Product.find(id)
        product.auditing_status = '驳回'
        product.reject_reason = params[:reson]
        product.auditor_id = session[:auditor_id]
        product.save
      end
      info = 'success'
    rescue Exception=>e
      logger.info e.to_s
      info = '驳回产品时出错，请重新操作。'
    end
    result = "{success:true,info:\"#{info}\"}"
    render :text=>result, :layout=>false
  end

  #得到产品驳回原因，审批人，申请人，都可能用到。
  def getRejectReasonOfProduct
    product = Product.find(params[:id])
    render :text=>product.to_json(:only => [:id,:reject_reason]),:layout=>false
  end
end
